from sqlalchemy import create_engine, func
from sqlalchemy import Column, Integer, TIMESTAMP, MetaData, VARCHAR

from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

import config


metadata = MetaData()

Base = declarative_base(metadata=metadata)

db = 'mysql://%s:%s@%s:%d/%s?charset=utf8' % (config.DB_USER, config.DB_PWD, config.DB_HOST, config.DB_PORT, config.DB_NAME)


def get_session():
    engine = create_engine(db, encoding="utf-8", echo=False)
    mysql_session_maker = sessionmaker(bind=engine)
    session = mysql_session_maker()
    return session

sql_session = get_session()


class Demand(Base):
    __tablename__ = 'demand'
    demand_id = Column('demand_id', Integer, autoincrement=True, primary_key=True)
    state = Column('state', Integer, nullable=False, default=0)
    stu_name = Column('stu_name', VARCHAR(20), nullable=False, default='')
    stu_id = Column('stu_id', VARCHAR(20), nullable=False, default='')
    stu_phone = Column('stu_phone', VARCHAR(20), nullable=False, default='')
    parent_phone = Column('parent_phone', VARCHAR(20), nullable=False, default='')
    start_time = Column('start_time', TIMESTAMP, nullable=False, server_default=func.NOW())
    stop_time = Column('stop_time', TIMESTAMP, nullable=False, server_default=func.NOW())
    apply_time = Column('apply_time', TIMESTAMP, nullable=False, server_default=func.NOW())
    approve_time = Column('approve_time', TIMESTAMP, nullable=True, server_default=func.NOW())
    reason = Column('reason', VARCHAR(255), nullable=False, default='')
    location = Column('location', VARCHAR(255), nullable=False, default='')
    director_comment = Column('director_comment', VARCHAR(255), nullable=True, default='')
    school_comment = Column('school_comment', VARCHAR(255), nullable=True, default='')

if __name__ == '__main__':
    print(sql_session.query(Demand).count())
